package com.cdtu.service.impl;

import com.cdtu.dao.NewsMapper;
import com.cdtu.entity.News;
import com.cdtu.entity.NewsColumn;
import com.cdtu.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;




    /*新闻查询*/
    @Override
    public PageInfo<News> getNewsTitle(Integer pageNum, Integer pageSize,
                                       Integer columnId, String source, String newsDate) throws ParseException {
        PageHelper.startPage(pageNum, pageSize);
        List<News> news;
        if (newsDate == null)
            news = newsMapper.selectByXml(columnId, source, null);
        else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            Date date = null;
            date = sdf.parse(newsDate);
            news = newsMapper.selectByXml(columnId, source, date);
        }
        return new PageInfo<>(news);
    }

    @Override
    public List<Map<String, Object>> getYearList() {
        return newsMapper.selectYearCount();
    }

    @Override
    public List<String> getSourceList() {
        return newsMapper.selectSourceCount();
    }

    @Override
    public List<Map<String, Object>> getColumnList() {
//        Map<String, Object> stringObjectMap = new HashMap<>();
//        stringObjectMap = newsMapper.selectColumnCount();
//        List<Map<String,Object>> objects = new ArrayList<>();
//        return objects;
        return newsMapper.selectColumnCount();
    }

    @Override
    public Map<String, Object> getNewsById(Integer newsId) {
        Map<String, Object> map = newsMapper.selectByNewsId(newsId);
        Object news_date = map.get("news_date");
        Date date = (Date)news_date;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd mm:DD:ss");
        String format = sdf.format(date);
        map.put("news_date",format);
        return map;
    }

    @Override
    public int deleteById(Integer newsId) {
        return newsMapper.deleteByPrimaryKey(newsId);
    }

    @Override
    public List<NewsColumn> getColumnListName() {
        return newsMapper.selectColumnName();
    }

    @Override
    public int add(News news) {
        return newsMapper.insert(news);
    }

    @Override
    public News getUpdateNewsById(Integer newsId) {
        return newsMapper.selectByPrimaryKey(newsId);
    }

    @Override
    public int update(News news) {
        return newsMapper.updateByPrimaryKeySelective(news);
    }


}
